/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.qingyan.flink.core.trans;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.qingyan.flink.core.properties.FlinkProperties;
import com.qingyan.flink.core.properties.FlinkTransProperties;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * Operations
 *
 * @author wenmo
 * @since 2021/5/25 15:50
 **/
@Slf4j
public class Operations {

    private Operations() {
    }

    public static List<Operation> getOperations(FlinkProperties properties) {
        Assert.notEmpty(properties.getDatasource(), "datasource not empty");
        Assert.notEmpty(properties.getTrans(), "trans not empty");
        Map<String, FlinkTransProperties> trans = properties.getTrans();
        ServiceLoader<Operation> operations = ServiceLoader.load(Operation.class);
        List<Operation> operationList = new ArrayList<>(trans.size());
        for (Map.Entry<String, FlinkTransProperties> entry : trans.entrySet()) {
            FlinkTransProperties value = entry.getValue();
            value.setName(entry.getKey());
            Operation op = null;
            for (Operation operation : operations) {
                op = operation.create(properties, value);
                if (op != null) {
                    break;
                }
            }
            Assert.notNull(op, entry.getKey() + " has not match trans operation");
            operationList.add(op);
        }
        return operationList;
    }
    // 下游

    protected static Operation match(Iterator<Operation> iterator, FlinkTransProperties properties) {
        while (iterator.hasNext()) {
            Operation operation = iterator.next();
            String strategy = properties.getStrategy();
            return operation;
        }
        return null;
    }

    public static void valid(List<Operation> operations) {
        StringBuilder sb = new StringBuilder();
        for (Operation operation : operations) {
            String valid = operation.valid();
            if (StrUtil.isNotEmpty(valid)) {
                sb.append(valid).append("\n");
            }
        }
        if (sb.length() > 0) {
            throw new IllegalArgumentException("数据源异常：\n" + sb.toString());
        }
    }


}
